home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 8.1 KB | 300 lines |
- /*
- * ButtonCell.java 1.0 12 Jan 1997
- *
- * Copyright (c) 1996 Krumel & Associates, Inc. All Rights Reserved.
- *
- * This software is provided as is. Krumel & Associates shall not be liable
- * for any damages suffered by licensee as a result of using, modifying or
- * distributing this software or its derivatives.
- */
-
- package symantec.itools.db.awt;
-
- import java.awt.*;
- import java.awt.image.ImageObserver;
-
- public class ButtonCell implements TableCell, ImageObserver {
- Grid view;
- DataSource dataSource;
- Coordinate coords;
- boolean pressed;
- boolean drawUp = true;
- boolean defaultFlag;
- int type;
-
- static final int PADSIDES = 5;
-
- //this one is friendly
- ButtonCell() {}
-
- public ButtonCell(Grid v, DataSource ds) {
- view = v;
- dataSource = ds;
- }
-
- //one relative
- public ButtonCell(int r, int c) {
- coords = new Coordinate(r-1, c-1);
- }
-
- public int type() {
- return type;
- }
-
- public int type(int t) {
- if (t > CORNER_CELL || t < 0) {
- throw new IllegalArgumentException("Invalid cell type");
- }
-
- return type = t;
- }
-
- public TableCell cloneCell() {
- ButtonCell bc = new ButtonCell(view, dataSource);
-
- if (coords != null) {
- bc.coords = new Coordinate(coords);
- }
- bc.type = type;
- bc.defaultFlag = defaultFlag;
-
- return bc;
- }
-
- public void setGrid(Grid v, DataSource ds) {
- view = v;
- dataSource = ds;
- }
-
- public void setDefaultFlag() { defaultFlag = true; }
-
- public void reset() {
- pressed = false;
- drawUp = true;
- }
-
- public void setCoordinates(Coordinate p) {
- coords = p;
- }
-
- public Coordinate getCoordinates() {
- return coords;
- }
-
- public int row() { return coords.row; }
-
- public void setRow(int r) { coords.row = r; }
-
- public int col() { return coords.col; }
-
- public void setCol(int c) { coords.col = c; }
-
- boolean inside(int x, int y) {
- Rectangle r = view.getCellBounds(this);
-
- if (x > 0 && y > 0 && x < r.width && y < r.height) {
- return true;
- }
-
- return false;
- }
-
- public boolean mouseEvent(Event e) {
- if (e.id == Event.MOUSE_DOWN) {
- view.setCapture(this);
- pressed = true;
- drawUp = false;
- view.generateEvent(e, view.BUTTON_DOWN_EVENT, this);
- view.redrawCell(this);
- } else if (e.id == Event.MOUSE_DRAG) {
- if (inside(e.x, e.y) && drawUp) {
- drawUp = false;
- view.generateEvent(e, view.BUTTON_FLICKER_DOWN_EVENT, this);
- view.redrawCell(this);
- } else if (!inside(e.x, e.y) && !drawUp) {
- drawUp = true;
- view.generateEvent(e, view.BUTTON_FLICKER_UP_EVENT, this);
- view.redrawCell(this);
- }
- } else if (e.id == Event.MOUSE_UP) {
- if (pressed && !drawUp) {
- view.generateEvent(e, view.BUTTON_UP_EVENT, this);
- }
-
- pressed = false;
- drawUp = true;
- view.redrawCell(this);
- view.lostCapture();
- }
-
-
- return true;
- }
-
- public Data getData() throws DataNotAvailable {
- return dataSource.getData(coords);
- }
-
- String chopString(String text, FontMetrics fm, Rectangle r) {
- int w = r.width;
- int i = 1;
- String t;
-
- if (text.length() == 0) { return text; }
-
-
- do {
- t = text.substring(0, i);
- } while(fm.stringWidth(t) < w - PADSIDES*2 && i++ < text.length());
-
- return text.substring(0, i-1);
- }
-
- public void drawCell(Graphics g, CellHints hints) {
- Data data;
-
- try {
- data = getData();
- } catch (DataNotAvailable ex) {
- data = new ImageStringData(dataSource, "");
- }
-
- Rectangle r = hints.bounds();
- FontMetrics fm = g.getFontMetrics();
- int asc = fm.getAscent() + 1;
- String text = chopString(data.toString(), fm, r);
- int sw = fm.stringWidth(text);
- int imageOffset = 0;
- Color fg = g.getColor();
- int origX = r.x;
-
- drawButton(g, r);
- Image im = data.toImage();
- switch(hints.alignment()) {
- case Grid.LEFT:
- if (im != null) {
- imageOffset = im.getWidth(this) + PADSIDES;
- }
-
- //check to see if it will all fit within cell
- if (imageOffset+sw+2+PADSIDES <= r.width && im != null) {
- r.x += PADSIDES;
- r.y += 3;
- g.drawImage(im, r.x, r.y, this);
- r.y -= 3;
- } else {
- imageOffset = PADSIDES;
- }
-
- r.x = origX + imageOffset + 2;
- break;
- case Grid.CENTER:
- r.x = origX + (r.width-sw)/2;
- break;
- case Grid.RIGHT:
- if (im != null) {
- imageOffset = im.getWidth(this) + PADSIDES;
- }
- //check to see if it will all fit within cell
- if (sw+imageOffset+2+PADSIDES <= r.width && im != null) {
- r.x = r.x + r.width - sw - PADSIDES - imageOffset - 2;
- r.y += 3;
- g.drawImage(im, r.x, r.y, this);
- r.y -= 3;
- }
-
- r.x = origX+r.width-sw-PADSIDES;
- break;
- }
-
- g.setColor(hints.foreground());
- g.drawString(text, r.x, r.y+fm.getAscent()+2);
- }
-
- void drawButton(Graphics g, Rectangle r) {
- //all highlighted and ready for delete
- Color bg = Color.lightGray;
-
- g.setColor(bg);
- g.fillRect(r.x,
- r.y,
- r.width-1,
- r.height);
-
- //draw shade for button
- g.setColor(Color.gray);
- g.drawRect(r.x+1, r.y+1, r.width-3, r.height-3);
- g.setColor(Color.black);
- g.drawRect(r.x, r.y, r.width-1, r.height-1);
-
- //draw the hightlight
- if (drawUp) {
- //draw button raised
- g.setColor(Color.white);
- g.drawLine(r.x+1, r.y+1, r.x + r.width-2, r.y+1);
- g.drawLine(r.x+1, r.y+1, r.x+1, r.y + r.height - 2);
- } else {
- //draw button recessed
- g.setColor(Color.white);
- g.drawLine(r.x+1, r.y + r.height - 2, r.x + r.width-2, r.y + r.height-2);
- g.drawLine(r.x + r.width - 2, r.y+1, r.x + r.width - 2, r.y + r.height - 3);
- }
- }
-
- public boolean isCellTypeEditable() { return false; }
-
- public boolean actionEvent(Event e) {
- return true;
- }
-
- public boolean keyEvent(Event e) {
- return true;
- }
-
- public boolean canLoseFocus() { return true; }
-
- public boolean focusEvent(Event e) {
- if (e.id == Event.GOT_FOCUS) {
- view.setCapture(this);
- view.generateEvent(e, view.GOT_CELL_FOCUS, this);
- } else {
- view.lostCapture();
- view.generateEvent(e, view.LOST_CELL_FOCUS, this);
- }
-
- return true;
- }
-
- public boolean loseFocusOnArrow() {
- return false;
- }
-
- public void activateCursor() {
- }
-
- public void deactivateCursor() {
- }
-
- public String stats() {
- return "";
- }
-
- /**
- * Repaints the list when the cell's image has changed.
- * @return true if image has changed; false otherwise.
- */
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
- if ((flags & (ABORT|ERROR)) != 0) {
- return false;
- }
-
- if ((flags & ALLBITS) != 0) {
- view.redrawAsync();
- return false;
- } else {
- return true;
- }
- }
- }
-
-